package com.besall.allbase.view.activity.chipstoollevel4.protocol.fragments;

import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import  bb.lanxing.R;


/**
 * A simple {@link Fragment} subclass.
 */
public class Fitness extends Fragment implements View.OnClickListener {

    public String TAG = "Fitness";
    private LinearLayout infoView1;
    private LinearLayout infoView2;
    public String[] cmds_1 = {"get fitness value", "get ECG HR &RR histroy data", "get auto HR & Bp history", "get history temp data","get 5 minutes walking data",
            "Get Hourly walking data","Get 10 minutes sleep data","get spo2 raw data for 125hz","get latest health data","Get 1 min sleep data","Get 5 minutes running step data",
    "Get climbing stairs historical data","Get 1 minutes cycling history data","Get active minutes history data","Get history data for automatic RR interval measurement",
    "Get GPS data","Obtain the hr and bp values","Get proximity alert multi-packet datas","Get live temperature value","Get contact tracing summary","Get contact tracing specified data",
    "Get history ppg raw data","Get history acc raw data","Get sedentary alert data","Get activity configured","Get activity configured new","Get activity summary data",
    "Get activity details data","Get SpO2 periodic measurement para","Get SpO2 periodic measurement data","Get sleep target value","Get sleep summary data","Get SensAI activity configured",
    "Get SensAI activity summary data","Get SensAI activity details data","Get history distance Data","Get history calorie Data","Get today fitness value","Get stress historical data",
    "Get firmware capability","get 4H button function","get activity guide metric"};
    public String[] cmds_2 = {"Set Daily Walking Target", "Set auto HR time interval", "Reporting ECG raw data", "HR Live measurement control","spo2 Live measurement control",
    "Erase Fitness Data","BP measurement control","Set the current sport mode","Update VO2 Max value to device","Update HR zone value to device","Set live steps control",
    "gps control","Set sport mode pause","Sync BP params","Clear user bp data","Clear contact tracing data","Set auto ppg data interval","Set auto acc raw data interval",
    "set activity function","delete activitys","delete activity record","set SpO2 periodic measurement","Set sleep target value","config stress feature control info",
    "HAR control config","Set SensAI activity config","Start a designated activity"," set 4H button function","set activity guide metric"};
    private Context mContext;

    FitnessCallBack mCallBack;
    public Fitness() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mContext = inflater.getContext();

        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_fitness, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        infoView1 = getActivity().findViewById(R.id.fitness_view1);
        infoView2 = getActivity().findViewById(R.id.fitness_view2);
        DisplayMetrics screen = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(screen);
        int width = screen.widthPixels;

        for (int i = 0; i < cmds_1.length; i++) {
            View view = new View(mContext);
            view.setId(i);
            view.setLayoutParams(new ViewGroup.LayoutParams((int) (width * 0.45), 20));

            Button button = new Button(mContext);
            button.setText("    " + cmds_1[i]);
            button.setId(i);
            button.setOnClickListener(this);
            button.setTextSize(12);
            button.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
            button.setGravity(Gravity.CENTER_VERTICAL);
            button.setBackgroundResource(R.drawable.rectangle_cardbtn);


            infoView1.addView(button);
            infoView1.addView(view);
        }

        for (int i = 0; i < cmds_2.length; i++) {
            View view = new View(mContext);
            view.setId(i);
            view.setLayoutParams(new ViewGroup.LayoutParams((int) (width * 0.45), 20));

            Button button = new Button(mContext);
            button.setText("    " + cmds_2[i]);
            button.setId(i + 100);
            button.setOnClickListener(this);
            button.setTextSize(12);
            button.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
            button.setGravity(Gravity.CENTER_VERTICAL);
            button.setBackgroundResource(R.drawable.rectangle_cardbtn);

            infoView2.addView(button);
            infoView2.addView(view);
        }
    }

    Toast toast;
    @Override
    public void onClick(View v) {
        if (mCallBack != null){
            mCallBack.onFitnessProtocolClick(v.getId());
            Log.i(TAG, "onClick: " + v.getId());
            int id = v.getId();
            if (toast != null)
                toast.cancel();
            if (id < 100){
                toast = Toast.makeText(mContext, cmds_1[id],Toast.LENGTH_SHORT);
                toast.show();
            } else if (id > 100) {
                toast = Toast.makeText(mContext, cmds_2[id - 100],Toast.LENGTH_SHORT);
                toast.show();
            }
        }
    }

    public void setFitnessCallback(FitnessCallBack fitnessCallback){
        mCallBack = fitnessCallback;
    }

    public interface FitnessCallBack{

        void onFitnessProtocolClick(int id);
    }
}
